package com.gitee.jisong;

import com.gitee.jisong.typeBuilder.TypeBuilder;
import com.gitee.jisong.typeBuilder.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author wb.sunjisong18 2020-06-09
 */
public class GSON {
    public static final TimeZone defaultTimeZone = TimeZone.getDefault();
    public static final Locale defaultLocale = Locale.getDefault();
    public static final String DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final Gson gson = new  GsonBuilder().setDateFormat(DEFFAULT_DATE_FORMAT).create();

    public static <T> T parseObject(String json, Type type) {
        return gson.fromJson(json,type);
    }

    public static <T> T parseObject(String json, Class<T> clazz) {
        return gson.fromJson(json,clazz);
    }

    public static GSONMap parseMap(String json) {
        return gson.fromJson(json, TypeBuilder.newInstance(GSONMap.class).build());
    }

    public static String toJSONString(Object obj) {
        return gson.toJson(obj);
    }

    public static <T> List<T> parseArray(String json, Class<T> clazz){
        return parseObject(json, TypeBuilder.newInstance(List.class).addTypeParam(clazz).build());
    }

    public static GSONArray parseArray(String json){
        return parseObject(json,TypeBuilder.newInstance(GSONArray.class).build());
    }

}
